#pragma comment(linker,"/STACK:100000000")
#include "testlib.h"
#include <stdio.h>
#include <string>
using namespace std;
#define N 55
int m[N][N];
int main(int argc, char * argv[])
{
    registerTestlibCmd(argc, argv);
    int n, i, j, k;
	string s;
	n=inf.readInt();
	for(i=0; i<n; i++)
		for(j=0; j<n; j++)
			m[i][j]=inf.readInt();
	for(i=0; i<n; i++)
	{
		for(j=0; j<n && m[i][j]; j++);
		if(j<n) break;
	}
	s=ouf.readWord();
	if(s=="No")
	{
		s=ans.readWord();
		if(s=="No") quitf(_ok, "answer is No");
		else quitf(_wa, "expected Yes, found No");
	}
	else if(s=="Yes")
	{
		if(ouf.seekEof()) s="";
		else s=ouf.readWord();
		if(s.size()>2500000) quitf(_wa, "number of moves must not be greater than 2500000");
		for(k=0; k<s.size(); k++)
			if(s[k]=='L')
			{
				if(j==0) quitf(_wa, "invalid move");
				else { swap(m[i][j], m[i][j-1]); j--; }
			}
			else if(s[k]=='R')
			{
				if(j==n-1) quitf(_wa, "invalid move");
				else { swap(m[i][j], m[i][j+1]); j++; }
			}
			else if(s[k]=='U')
			{
				if(i==0) quitf(_wa, "invalid move");
				else { swap(m[i][j], m[i-1][j]); i--; }
			}
			else if(s[k]=='D')
			{
				if(i==n-1) quitf(_wa, "invalid move");
				else { swap(m[i][j], m[i+1][j]); i++; }
			}
			else quitf(_pe, "invalid direction %c", s[k]);
		for(i=0; i<n; i++)
			for(j=0; j<n; j++)
				if(m[i][j]!=(i*n+j+1)%(n*n)) quitf(_wa, "puzzle is not solved");
		s=ans.readWord();
		if(s=="Yes") quitf(_ok, "answer is Yes, %d moves", k);
		else quitf(_fail, "contestant found solution, but jury didn't");
	}
	else quitf(_pe, "invalid answer");
}